package com.android.clinic.app.ui.doctor;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.clinic.app.R;
import com.android.clinic.app.adapter.BaseListAdapter;
import com.android.clinic.app.adapter.DoctorAdapter;
import com.android.clinic.app.base.BaseListActivity;
import com.android.clinic.app.entity.DoctorInfo;
import com.android.clinic.app.entity.DoctorListInfo;
import com.android.clinic.app.http.HttpClient;
import com.android.clinic.app.http.base.BaseObserver;
import com.android.clinic.app.http.utils.RxObservableUtils;
import com.android.clinic.app.ui.widget.UIAlertView;
import com.android.clinic.app.utils.Constants;
import com.android.clinic.app.utils.LocalDataUtils;
import com.android.clinic.app.utils.Utils;
import com.kennyc.view.MultiStateView;

import java.util.List;

/**
 * Created by cheng on 2018/2/5.
 * 全部名医
 */
public class ListAllDoctorActivity extends BaseListActivity<DoctorInfo> {

    private String deptName, hospital = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_listview;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBarTitle("全部名医");
        setTitleBarLeftResource(R.drawable.icon_back);

        deptName = this.getIntent().getStringExtra("deptName");
        hospital = this.getIntent().getStringExtra("hospital");

        mAdapter.setOnItemClickListener((view, position) -> {
            String doctorId = LocalDataUtils.getData(Constants.QUESTION_ID);
            // 如果doctorId不为null，说明该用户没有对医生进行提问
            if (!Utils.isEmpty(doctorId)) {
                UIAlertView.alertDialog(ListAllDoctorActivity.this, "操作提示",
                        "您还有一个提问待确认?", new UIAlertView.OnThemeDialogListener() {
                            @Override
                            public void OnDismissButton() {
                                // 用户放弃提问，清除缓存，跳转到医生详情
                               LocalDataUtils.removeData(Constants.QUESTION_ID);
                                startDoctorDetailActivity(mAdapter.getItem(position));
                            }
                            @Override
                            public void OnSureButton() {
                                // 去提问页面，继续提问
                                Intent intent = new Intent(ListAllDoctorActivity.this, AskQuestionWithDoctorActivity.class);
                                intent.putExtra("doctorId", doctorId);
                                startActivity(intent);
                            }
                        },"继续提问","放弃提问");

            } else {
                startDoctorDetailActivity(mAdapter.getItem(position));
            }
        });

    }

    /**
     * 跳转到医生详情
     * @param doctorInfo
     */
    private void startDoctorDetailActivity(DoctorInfo doctorInfo) {
        Intent intent = new Intent(ListAllDoctorActivity.this, DoctorDetailActivity.class);
        intent.putExtra("doctorId", doctorInfo.getId());
        intent.putExtra("reservation", doctorInfo.getReservation());
        intent.putExtra("evaluation", doctorInfo.getEvaluation());
        startActivity(intent);
    }

    @Override
    protected void reloadData() {
        HttpClient.getHttpService().getListAllDoctor(deptName, hospital, pageSize, pageNum)
                .compose(this.bindToLifecycle())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<DoctorListInfo>() {
                    @Override
                    public void onSuccess(DoctorListInfo doctorListInfo) {
                        refreshComplete();
                        if (doctorListInfo.getResult().size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);

                            List<DoctorInfo> doctorInfoList = doctorListInfo.getResult();
                            int doctorSize = doctorInfoList.size();
                            for (int i = 0 ;i < doctorSize; i++) {
                                doctorInfoList.get(i).setReservation(String.valueOf((int)(Math.random()*1000)));
                                doctorInfoList.get(i).setEvaluation(String.valueOf((int)(Math.random()*100)));
                            }
                            mAdapter.addAll(doctorInfoList);
                        } else if (pageNum == 1) {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        refreshComplete();
                        showToast(message);
                    }
                });

    }

    @Override
    protected BaseListAdapter<DoctorInfo> getAdapter() {
        return new DoctorAdapter(this);
    }

}
